<?php
class ArticleController extends Controller {
    public function actionAbout(){
        echo 1;
    }

    public function actionIndex()
    {
        $this->redirect(Yii::app()->homeUrl, true, 301);
    }
    public function actionView()
    {
        $url_key = $_GET['url_key'];
        $article = Article::model()->findByAttributes(
                    array('url_key'=>$url_key,'status'=>Article::STATUS_AVAILABLE),
                    array('select'=>'id,title,url_key,category_ids,primary_category,content,short_content,created_time,updated_time,author')
                );
        if($article){
            $article->hits = $article->hits + 1;
            $article->update();
            if($article->primary_category > 0)
                $category = ArticleCategory::model()->findByPk(
                                $article->primary_category,
                                array('select'=>'id,parent_id,name,url_key','limit'=>'0,1')
                            );

            if($category){
            //Start update metadata
            $parentCategoryNames = array();
            $parentCategory = ArticleCategory::getParent($category);
            while($parentCategory->id > 0)
            {
                $parentCategoryNames[] = $parenCategory->name;
                $parentCategory = ArticleCategory::getParent($parentCategory);
            }
            $htmlMetadata = ArticleMetaDataPeer::model()->findByAttributes(array('article_id'=>$article->id));
            if($htmlMetadata->title) $this->htmlMetadata->title = $htmlMetadata->title;
            else{
                if(count($parentCategoryNames)>0){
                    $this->htmlMetadata->title = implode('-', $parentCategoryNames)."-".$category->name;
                }
                else{
                    $this->htmlMetadata->title = $category->name;
                }
            }
            if($htmlMetadata->keywords) $this->htmlMetadata->keywords = $htmlMetadata->keywords;
            else $this->htmlMetadata->keywords .= ', '.$category->name;
            if($htmlMetadata->description) $this->htmlMetadata->description = $htmlMetadata->description;
            elseif($category->description) $this->htmlMetadata->description = strip_tags($category->description);
            else $this->htmlMetadata->description .= ', '.$category->name;
            //End update metadata
            }
            else
            {
                $htmlMetadata = ArticleMetaDataPeer::model()->findByAttributes(array('article_id'=>$article->id));
                $tile = Common::makeFriendlyUrl($article->title);
                if($htmlMetadata->title) $this->htmlMetadata->title = $htmlMetadata->title;
                else{
                    $this->htmlMetadata->title = $tile;
                }
                if($htmlMetadata->keywords) $this->htmlMetadata->keywords = $htmlMetadata->keywords;
                else $this->htmlMetadata->keywords .= ', '.$tile;
                if($htmlMetadata->description) $this->htmlMetadata->description = $htmlMetadata->description;
                else $this->htmlMetadata->description .= ', '.$tile;
            }

            //Get article images
            $images = $article->getImages();
            
            $oldS = Yii::app()->session['wasreads'];
            if($oldS){
                $oldarr = explode(',', $oldS);
                if(!in_array($article->id, $oldarr))
                        Yii::app()->session['wasreads'] = $oldS.','.$article->id;
            }else
               Yii::app()->session['wasreads'] = $article->id;
                
                
            // get comment list
            $sortField  = (isset(Yii::app()->params->commentsSortField))? Yii::app()->params->commentsSortField : 'created_time';
            $sortDir    = (isset(Yii::app()->params->commentsSortDir))? Yii::app()->params->commentsSortDir : 'asc';
            $limit      = (isset(Yii::app()->params->commentsLimit))? Yii::app()->params->commentsLimit : 2;

            $filters = array();
            $filters['page'] = (isset($_GET['page']) && $_GET['page'])?$_GET['page']:1;
            $filters['sort'] = "$sortField $sortDir";
            $filters['limit'] = $limit;
        }
        else{$this->redirect(Yii::app()->homeUrl, true, 301);}

        $this->layout = "article";
        $this->render('article', array(
                'article'=>$article,
                'category'=>$category,
                'images'=>$images,
        ));
        Yii::app()->end();
    }
    
    public function actionList()
    {
        $category_url = $_GET['c']?$_GET['c']:'';
        $category = ArticleCategory::model()->findByAttributes(array("url_key"=>$category_url),array('select'=>'id,name,url_key'));

        $filters = array();
        $filters['distinct'] = true;
        $filters['select'] = 'id, title, primary_category, url_key, short_content, created_time, updated_time, updated_by, sorder';
        $filters['index'] = 'id';
        $page = (isset($_GET['page']) && $_GET['page'])?$_GET['page']:1;
        $filters['page'] = $page;
        $filters['limit'] =10;
        $filters['sort'] = "sorder desc";

        $article = new Article();
        $article->status = Article::STATUS_AVAILABLE;
        $article->is_hot = '';
        $article->is_feature = '';
        $article->primary_category = $category->id;
        $dataProvider = $article->search($filters);

        if($category)
        {
            //Start update metadata
            $htmlMetadata = ArticleCategoryMetaDataPeer::model()->findByPk($category->id,array('select'=>'title,keywords,description'));
            if($htmlMetadata->title) $this->htmlMetadata->title = $htmlMetadata->title;
            else
            {
                $this->htmlMetadata->title .= " - ".$category->name;
            }
            if($htmlMetadata->keywords) $this->htmlMetadata->keywords = $htmlMetadata->keywords;
            else $this->htmlMetadata->keywords .= ', '.$category->name;

            if($htmlMetadata->description) $this->htmlMetadata->description = $htmlMetadata->description;
            else if($category->description) $this->htmlMetadata->description = strip_tags($category->description);
            else $this->htmlMetadata->description .= ', '.$category->name;
            //End update metadata

            $this->layout = "articles";
            $this->render('articles',array(
                'category'=>$category,
                'dataProvider'=>$dataProvider,
            ));
        }
        else{
			$this->layout = "articles";
			$this->render('articles',array(
                'category'=>$category,
                'dataProvider'=>$dataProvider,
            ));
		}
        Yii::app()->end();
    }

    public function actionCategory()
    {
        $sortField  = (isset(Yii::app()->params->articleNewListSortField))? Yii::app()->params->articleNewListSortField : 'sorder';
        $sortDir    = (isset(Yii::app()->params->articleNewListSortDir))? Yii::app()->params->articleNewListSortDir : 'desc';
        
		$category_url = $_GET['url_key'];
        $category = ArticleCategory::model()->findByAttributes(array("url_key"=>$category_url));
		
		$filters = array();
        $page = (isset($_GET['page']) && $_GET['page'])?$_GET['page']:1;
        $filters['page'] = $page;
        $filters['sort'] = "$sortField $sortDir";

        $article = new Article();
        $article->status = Article::STATUS_AVAILABLE;
        $article->is_hot = '';
        $article->primary_category = $category->id;
        $dataProvider = $article->search($filters);

        if($category)
        {
            //Start update metadata
            $htmlMetadata = ArticleCategoryMetaDataPeer::model()->findByPk($category->id);
            if($htmlMetadata->title) $this->htmlMetadata->title = $htmlMetadata->title;
            else
            {
                $this->htmlMetadata->title .= " - ".$category->name;
            }
            if($htmlMetadata->keywords) $this->htmlMetadata->keywords = $htmlMetadata->keywords;
            else $this->htmlMetadata->keywords .= ', '.$category->name;

            if($htmlMetadata->description) $this->htmlMetadata->description = $htmlMetadata->description;
            else if($category->description) $this->htmlMetadata->description = strip_tags($category->description);
            else $this->htmlMetadata->description .= ', '.$category->name;
            //End update metadata

            $this->layout = "articles";
            $this->render('articles',array(
                'category'=>$category,
                'dataProvider'=>$dataProvider,
            ));
		}
        Yii::app()->end();
    }
	public function actionListCat()
	{
		$sortField  = (isset(Yii::app()->params->articleCategoriesSortField))? Yii::app()->params->articleCategoriesSortField : 'sorder';
        $sortDir    = (isset(Yii::app()->params->articleCategoriesSortDir))? Yii::app()->params->articleCategoriesSortDir : 'desc';
        $limit      = (isset(Yii::app()->params->articleCategoriesLimit))? Yii::app()->params->articleCategoriesLimit : 3;
		
		$this->htmlMetadata->title = 'Tin tức - ' .$this->htmlMetadata->title;
		$this->htmlMetadata->keywords = 'Tin tức - ' .$this->htmlMetadata->title;
		$this->htmlMetadata->description = 'Tin tức - ' .$this->htmlMetadata->title;

		$filters = array();
        $filters['page'] = (isset($_GET['page']) && $_GET['page'])?$_GET['page']:1;
        $filters['sort'] = "$sortField $sortDir";
        $filters['limit'] = $limit;
		$filters['selects']  = 'id,name,url_key,sorder';

        $model = new ArticleCategory();
        $model->type   = ArticleCategory::TYPE_NEWS;
        $model->status = ArticleCategory::STATUS_AVAILABLE;
        $dataProvider = $model->search($filters);
		
        $this->layout = "articles";
		$this->render('categories',array(
			'dataProvider'=>$dataProvider,
		));
        Yii::app()->end();
	}
}
?>